package com.itheima.leetcode.od.c.greedy;

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * <h3>组合出合法最小数</h3>
 */
public class GenerateLegalMinimum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String[] strs = "08 10 2".split(" ");

        System.out.println(getResult(strs));
    }

    public static String getResult(String[] strs) {
        Arrays.sort(strs, (a, b) -> (a + b).compareTo(b + a));

        if (strs[0].charAt(0) == '0') {
            for (int i = 1; i < strs.length; i++) { // 还是贪心
                if (strs[i].charAt(0) != '0') {
                    strs[0] = strs[i] + strs[0];
                    strs[i] = "";
                    break;
                }
            }
        }

        String ans = Arrays.stream(strs)
                .collect(Collectors.joining(""))
                .replaceAll("^0+", "");

        // 避免返回空串
        return "".equals(ans) ? "0" : ans;
    }
}